package com.hutiao.snowhb.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.hutiao.snowhb.common.RandomId;
import com.hutiao.snowhb.common.WeixinUtil;
import com.hutiao.snowhb.dao.BaseDao;
import com.hutiao.snowhb.entities.WeixinUserInfo;
import com.hutiao.snowhb.entities.WeixinUserList;
import com.hutiao.snowhb.resource.ConfigInfo;

public class UpdateUserInfo {

	
	
	public static void main(String[] args) throws Exception {
		
		ApplicationContext context = new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml");
		BaseDao baseDao = (BaseDao) context.getBean("baseDao");
		
		ConfigInfo.wxAccessToken = "fbG7O2zyBnNWO1u4Jrbaqx4t78w4TehoxCoSnzJ0JqD146af80LW5lkupYoOovQc0x_6bhMQGV-ZsVcRrZwcSioiiCu05RiJNQwQAHyaTKdNH-cX7rkn4bMb1L4U7L6F7Q3S3aHHkZH2C7knvyxCKg";
		
		WeixinUserList list = WeixinUtil.getUserList();
		
		System.out.println("total:" + list.getTotal());
		System.out.println("count:" + list.getCount());
		System.out.println("next_openid:" + list.getNext_openid());
		System.out.println("userInfo:");
		
		for (String openid : list.getOpenid()) {
			WeixinUserInfo wxu = WeixinUtil.getUserInfo(openid);
			System.out.println(openid + "\t" + wxu.getNickname());
			
			if (wxu.getSubscribe() == 0) {
				continue;
			}
			
			RandomId r = new RandomId();
			int uid = Integer.parseInt(r.randomId(new java.util.Random().nextInt(100)));
			
			String sql = "insert into snow_user(uid,subscribe,openId,nickname,sex,"
					+ "city,country,province,"
					+ "language,headimgurl,subscribeTime,createTime,updateTime) "
					+ "values("+uid+","+wxu.getSubscribe()+",'"+wxu.getOpenId()+"','"+wxu.getNickname()+"',"+wxu.getSex()+","
					+ "'"+wxu.getCity()+"','"+wxu.getCountry()+"','"+wxu.getProvince()+"',"
					+ "'"+wxu.getLanguage()+"','"+wxu.getHeadimgurl()+"',FROM_UNIXTIME("+wxu.getSubscribe_time()+"),NOW(),NOW()) "
					+ "ON DUPLICATE KEY UPDATE subscribe="+wxu.getSubscribe()+",nickname='"+wxu.getNickname()+"',sex='"+wxu.getSex()+"', "
					+ "city='"+wxu.getCity()+"',country='"+wxu.getCountry()+"',province='"+wxu.getProvince()+"', "
					+ "language='"+wxu.getLanguage()+"',headimgurl='"+wxu.getHeadimgurl()+"',subscribeTime=FROM_UNIXTIME("+wxu.getSubscribe_time()+"),updateTime=NOW();";
			
			baseDao.execute(sql);
		}
	}
	
	
}
